// pages/address/address.js
import http from '../../utils/http.js'
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressList:[]
  },
  chooseAddress(e){
    let address = e.currentTarget.dataset.address;
    wx.setStorageSync("chooseAddress", address);
    wx.navigateBack();
  },
  goAddAddress(){
    wx.navigateTo({
      url: './insert_address',
    })
  },
  editAddress(e){
    let address = e.currentTarget.dataset.address;
    wx.navigateTo({
      url: './insert_address?addressInfo=' + JSON.stringify(address)
    })
  },
  delete(e){
    let me=this;
    wx.showModal({
      title: '操作提示',
      content: '确定要删除该收货地址吗？',
      success:res=>{
        if(res.confirm){
          http.request({
            url: 'user/deleteAddress/' + e.currentTarget.dataset.addressId,
            success:res=>{
              me.getUserAddress();
            }
          });
        }
      }
    })
  },
  setDefaultAddress(e){
    let me=this;
    let userId = wx.getStorageSync("userId");
    http.request({
      url: 'user/setDefaultAddress/' + e.currentTarget.dataset.addressId + "/" + userId,
      success:res=>{
        me.setData({
          addressList: res
        });
      }
    });
  },
  getUserAddress(){
    let me=this;
    let userId = wx.getStorageSync("userId");
    http.request({
      url:'user/getUserAddress/'+userId,
      method:'get',
      loadTip: '加载中',
      success:res=>{
        console.log(res);
        me.setData({
          addressList:res
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '我的收货地址'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserAddress();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})